package com.shsxt.crm.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shsxt.common.util.Result;
import com.shsxt.common.util.StringUtil;
import com.shsxt.crm.dao.YgRoleDao;
import com.shsxt.crm.dao.YgUserPermissionDao;
import com.shsxt.crm.model.YgRole;
import com.shsxt.crm.model.YgSystemMenuModel;
import com.shsxt.crm.model.YgUserPermission;
import com.shsxt.crm.service.ISystemMenuModeSerivce;
import com.shsxt.crm.service.ISystemRoleService;
import com.shsxt.framework.exception.YgException;

@Service
public class SystemRoleServiceImpl implements ISystemRoleService {

	@Autowired
	private YgRoleDao ygRoleDao;

	@Autowired
	private YgUserPermissionDao ygUserPermissionDao;

	@Autowired
	private ISystemMenuModeSerivce systemMenuModeSerivce;

	@Override
	public List<YgRole> queryAllRoles(String roleName) {

		Map<String, Object> params = new HashMap<>();
		if (StringUtil.isNotEmpty(roleName)) {
			params.put("roleName", roleName);
		}
		params.put("isValid", 1);
		List<YgRole> Roles = ygRoleDao.find(params);
		return Roles;
	}

	@Override
	public Map<String, Object> queryAllRolesByParams(String roleName, Integer page, Integer rows) {

		List<YgRole> roles = queryAllRoles(roleName);
		PageHelper.startPage(page, rows);

		PageInfo<YgRole> info = new PageInfo<>(roles);

		// 返回数据
		Map<String, Object> map = new HashMap<>();
		map.put("total", info.getTotal());
		map.put("rows", info.getList());
		return map;
	}

	@Override
	@Transactional
	public Result insertSysRole(YgRole ygRole) {

		ygRole.setIsValid(1);
		ygRole.setCreateDate(new Date());
		ygRole.setUpdateDate(new Date());
		long ste = ygRoleDao.saveSte(ygRole);// 添加角色
		if (ste == 1) {
			return Result.success("添加角色成功");
		}
		return Result.fail("添加角色失败");
	}

	@Override
	@Transactional
	public Result updateSysRole(YgRole ygRole) {
		ygRole.setUpdateDate(new Date());
		long ste = ygRoleDao.updateSte(ygRole);// 添加角色
		if (ste == 1) {
			return Result.success("更新角色成功");
		}
		return Result.fail("更新角色失败");
	}

	@Override
	@Transactional
	public Result delSysRole(Integer id) {
		YgRole ygRole = new YgRole();
		ygRole.setId(id);
		ygRole.setIsValid(0);
		return updateSysRole(ygRole);// 调用 更新
	}

	/**
	 * 思路： 权限表 1. roid 2. moduleIds 3. aclValue
	 * 
	 * 获取模块 aclValue ----> 通过 moduleId 查询 t_module
	 * 
	 * 授权： 1、删除已有权限 2、重现添加角色权限
	 */
	@Override
	@Transactional
	public Result addPermission(Integer rid, Integer[] moduleIds) {

		if (rid == null) {
			throw new YgException("500", "角色id不能为空");
		}
		ygRoleDao.delUserRoleByRid(rid);
		for (Integer mid : moduleIds) {
			YgSystemMenuModel model = systemMenuModeSerivce.queryMenuModelByMid(mid);
			YgUserPermission ygUserPermission = new YgUserPermission();
			ygUserPermission.setRoleId(rid);
			ygUserPermission.setModuleId(mid);
			ygUserPermission.setAclValue(model.getOptValue());
			ygUserPermission.setCreateDate(new Date());
			ygUserPermission.setUpdateDate(new Date());
			// 授权
			ygUserPermissionDao.save(ygUserPermission);
		}
		return Result.success("授权成功");
	}
}
